/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 *
 * @author Gebruiker
 */
public class TabbedPane extends JTabbedPane {
    

    public TabbedPane() {
        super();
        
    }

    public void addNewTab(Component component, String title) {
        this.add(title, component);
        int index = this.indexOfComponent(component);
        JPanel pnlTab = new JPanel(new GridBagLayout());
        pnlTab.setOpaque(false);
        JLabel lblTitle = new JLabel(title);
        lblTitle.setFont(new Font("Tahoma", 0, 14));
        
        JButton btnClose = new JButton("x");
        btnClose.setBorderPainted(false);
        btnClose.setFocusPainted(false);
        btnClose.setBorderPainted(false);
        btnClose.setContentAreaFilled(false);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;

        pnlTab.add(lblTitle, gbc);
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.gridx = 1;
        gbc.weightx = 0;
        pnlTab.add(btnClose, gbc);
        this.setTabComponentAt(index, pnlTab);
        btnClose.addActionListener(new MyCloseActionHandler(this, component));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(this.getTabCount() <= 0){
            Graphics2D g2d = (Graphics2D) g;
            BufferedImage image = null;
            try {
                //image = ImageIO.read(new File("C:/Users/Gebruiker/Desktop/bg.jpg"));
                image = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("images/bg.jpg"));
                int width = image.getWidth();
                int height = image.getHeight();
                int scale1 = width/this.getWidth()+1;
                int scale2 = height/ this.getHeight()+1;
                int scale = Math.max(scale1, scale2);
                BufferedImage resized = new BufferedImage(width/scale, height/scale, image.getType());
                g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g2d.drawImage(image, (this.getWidth()-width/scale)/2, (this.getHeight()-height/scale)/2, width/scale, height/scale, Color.WHITE, null);
            } catch (IOException ex) {
                Logger.getLogger(TabbedPane.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    

    private class MyCloseActionHandler implements ActionListener {

        private TabbedPane tabPane;
        private Component component;

        public MyCloseActionHandler(TabbedPane tabPane, Component component) {
            this.tabPane = tabPane;
            this.component = component;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {

            Component selected = tabPane;
            if (selected != null) {
                tabPane.remove(component);
            }

        }
    }
}
